//
//  SwitchViewController.m
//  MultiViewDemo
//
//  Created by Ngoc on 4/29/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SwitchViewController.h"
#import "StartScreenController.h"
#import <QuartzCore/CoreAnimation.h>

@implementation SwitchViewController
@synthesize currentController,previousController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void) switchView:(UIViewController *)currentCtrl toView:(UIViewController *)nextCtrl
{
    
    @try {
        //[UIView beginAnimations:nil context:NULL]; 
        //[UIView setAnimationDuration:1];
        UIView *theWindow = currentCtrl.view.superview;
        //[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        
        //[currentCtrl.view.superview insertSubview:nextCtrl.view atIndex:0];
        
        
       // [UIView setAnimationTransition: UIViewAnimationTransitionCurlUp
        //                       forView:theWindow cache:YES];
       // [currentCtrl viewWillDisappear:YES];
        //[nextCtrl viewWillAppear:YES];
        //nextCtrl.view.frame = currentCtrl.view.superview.frame;
        [currentCtrl.view removeFromSuperview];
        [theWindow addSubview:nextCtrl.view];
        //[currentCtrl viewDidDisappear:YES];	
        //[nextCtrl viewDidAppear:YES];
        //[UIView commitAnimations];
        
        CATransition *animation = [CATransition animation];
        [animation setDuration:1];
        [animation setType:kCATransitionFade];
        [animation setSubtype:kCATransitionFromLeft];
        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        
        [[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];
        
        // important note for problem with view shift after transition view
        nextCtrl.view.frame = [[UIScreen mainScreen]applicationFrame];
        
      //  self.currentController = nextCtrl;
      //  self.previousController = currentCtrl;
        
    }
    @catch (NSException *exception) {

    }
    @finally {
  
    }
    
}
-(void) goToPreviousView
{
    if (nil != self.currentController && nil != self.previousController) 
    {

    [self switchView:self.currentController toView:self.previousController];
        
    }
}

+(SwitchViewController*) sharedInstance
{
 static SwitchViewController* sharedInstance;
    if(nil == sharedInstance)
    {
        sharedInstance = [[SwitchViewController alloc] init];
    }
    return sharedInstance;
    
}
- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

-(void) viewDidLoad
{
//    self.subViewController = [[StartScreen alloc] initWithNibName:@"StartScreen" bundle:nil];
//    [self.view insertSubview:subViewController.view atIndex:0];
   // [startScreenController release];
    //parentViewController = self;
    [super viewDidLoad];
}
- (void)viewDidUnload
{
    
    [self.previousController release];
    [self.currentController release];
    [super viewDidUnload];
    
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
